<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
* 管理员组数据层
*/
class Admin_group_model extends CI_Model
{
	public $table;
	public $primary_key;

	public function __construct()
	{
		parent::__construct();
		$this->table = $this->db->dbprefix('admin_group');
		$this->primary_key = 'group_id';
	}
	/*
	* 跟据主键获取一条记录
	*/
	public function get_by_id($id, $select="*")
	{
		return (array)$this->db->query("select ".$select." from ".$this->table." where `".$this->primary_key."`='".$id."'")->row_array();
	}
	/*
	* 跟据字段获取一条记录
	*/
	public function get_by_field($field, $value, $select="*")
	{
		return (array)$this->db->query("select ".$select." from ".$this->table." where `".$field."`='".$value."'")->row_array();
	}
	/*
	* 列表记录集
	*/
	public function result_data($where = '', $order_by = '', $start = 0, $pagesize = 20)
	{
		$sql = "select * from ".$this->table." ".$where." ".$order_by." limit ".$start.",".$pagesize;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	/*
	* 所有记录
	*/
	public function result_all($where = '', $order_by = '', $select = "*")
	{
		$sql = "select ".$select." from ".$this->table." ".$where." ".$order_by;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	/*
	* 列表记录数
	*/
	public function result_count($where = '')
	{
		$sql = "select count(1) as total from ".$this->table." ".$where;
		$row = $this->db->query($sql)->row_array();
		return (int)$row['total'];
	}
	/*
	* 主健为索引结果数据
	*/
	public function result_maps($result)
	{
		if(empty($result)) return array();
		$data = array();
		foreach($result as $k=>$val) $data[$val[$this->primary_key]] = $val;
		return $data;
	}
	/*
	* 字段自增
	*/
	public function inc_field($id, $field)
	{
		$sql = "update ".$this->table." set `".$field."`=`".$field."`+1 where `".$this->primary_key."`='".$id."'";
		$this->db->query($sql);
		return true;
	}
	/*
	* 添加记录
	*/
	public function insert($insert)
	{
		$this->db->insert($this->table, $insert);
		$group_id = $this->db->insert_id();
		
		$row = $this->get_by_id($group_id);
		$update = array(
			'group_hash' => md5('group_id='.$row['group_id'].'&group_perm='.$row['group_perm'].'&hash='.SYS_KEY)
		);
		$this->db->update($this->table, $update, array('group_id'=>$group_id));
		
		return $group_id;
	}
	/*
	* 更新数据
	*/
	public function update($id, $update)
	{
		if($id < 1) return false;
		$where = array(
			$this->primary_key => $id,
		);
		$this->db->update($this->table, $update, $where);
		
		$row = $this->get_by_id($id);
		$update = array(
			'group_hash' => md5('group_id='.$row['group_id'].'&group_perm='.$row['group_perm'].'&hash='.SYS_KEY)
		);
		$this->db->update($this->table, $update, $where);
		return true;
	}
	/*
	* 删除数据
	*/
	public function delete($id)
	{
		if($id < 1) return false;
		$where = array(
			$this->primary_key => $id,
		);
		$this->db->delete($this->table, $where);
		return true;
	}
	/*
	* 检查权限安全
	*/
	public function chech_hash($group_id)
	{
		$group_id  = intval($group_id);
		$row       = $this->get_by_id($group_id);
		if(empty($row)) return false;
		$str = 'group_id='.$row['group_id'].'&group_perm='.$row['group_perm'].'&hash='.SYS_KEY;
		
		return md5($str)==$row['group_hash'];
	}
	/*
	* 检查权限
	*/
	public function check_perm($perm_name,$group_id)
	{
		if(empty($perm_name)) {
            return false;
        }
        $row       = $this->get_by_id($group_id);
		$group_perm  = $row['group_perm'];

		if(empty($group_perm))
		{
			return false;
		}
		$group_perm = json_decode($group_perm,true);
		return in_array($perm_name, $group_perm);
	}
	
	/*
	* 加载账号菜单
	*/
	public function load_menu($group_id)
	{
		$group_id   = intval($group_id);
		$row        = $this->get_by_id($group_id);
		if(empty($row) || !$this->chech_hash($group_id))
		{
			return array();
		}
		$group_perm = json_decode($row['group_perm'], true);
		if(!$group_perm) return array();
		
		$this->load->vars(array('group_perm' => $group_perm));
		
		$cur  = $this->load->get_var('cur');
		$breadcrumb = '当前位置：';
		$this->config->load('menu', true);
		$menu = $this->config->item('admin_menu', 'menu');
		foreach($menu as $name=>$val)
		{
			$found = in_array($val['name'], $group_perm);
			$menu[$name]['current'] = false;
			
			foreach($val['list'] as $k=>$v)
			{
				if(!in_array($v['name'], $group_perm))
				{
					unset($menu[$name]['list'][$k]);
				}
				else
				{
					$menu[$name]['list'][$k]['current'] = false;
					if($cur == $v['name'])
					{
						$menu[$name]['list'][$k]['current'] = true;
						$menu[$name]['current'] = true;
						$breadcrumb .= '<a href="javascript:;">'.$val['title'].'</a><span class="divider">/</span><a href="'.site_url($v['jump']).'">'.$v['title'].'</a>';
					}
					$found = true;
				}
			}
			if(!$found)
			{
				unset($menu[$name]);
			}
		}
		$this->load->vars(array('menuGroup' => $menu, 'breadcrumb'=>$breadcrumb));
		return $menu;
	}
}